package com.qixin.web.controller.User;


import com.qixin.api.bean.ComPeople;
import com.qixin.api.bean.ComPtUser;
import com.qixin.api.bean.FileResult;
import com.qixin.api.service.ComPeopleService;
import com.qixin.api.service.ComPtUserService;
import com.qixin.web.service.WeUserService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;

/**
 * <p>
 * 微信充电桩用户 前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2018-10-02
 */
@Controller
@RequestMapping("/weUser")
public class WeUserController {
    @Autowired
    WeUserService weUserService;
   @Autowired
   ComPtUserService comPtUserService;
    @Autowired
    ComPeopleService comPeopleService;
    private   final org.slf4j.Logger logger = LoggerFactory.getLogger(WeUserController.class);

    //获取手机验证码
     @RequestMapping("/SendSMS")
    @ResponseBody
    public String SendSMS(Long phone) {
       return  weUserService.SendSMS(phone);
    }

    //验证码校验  //// TODO: 2018/10/2 跳转页面model记得传手机号 用于设置缓存区
    @RequestMapping("/equalsCache")
    @ResponseBody
    public boolean equalsCache(String code,String phone) {
       return weUserService.equalsCache(code,phone);
    }


    //注册上传文件   a1 营业执照  b1身份证正面  b2 身份证反面  c1 驾驶证正面   c2驾驶证反面
    @RequestMapping("/fileUpload")
    @ResponseBody
    public FileResult fileUpload(MultipartFile file, String flag) {
      return   weUserService.fileUpload(file,flag);
    }

    //企业用户注册
    @RequestMapping("/insertPt")
    @ResponseBody
    public String insertPt(String param,String code,String url) {
        return  weUserService.insertPt(param,code,url);
    }

	//企业用户忘记密码
	@RequestMapping("/updatePt")
	@ResponseBody
	public String updatePt(String param,String code) {
		return  comPtUserService.updatept(param,code);
	}

	//个人用户注册
    @RequestMapping("/insertPeople")
    @ResponseBody
    public String insertPeople(String param,String code,String b1url,String b2url,String c1url,String c2url) {
        try {
            return  weUserService.insertPeople(param,code,b1url,b2url,c1url,c2url);
        } catch (SQLException e) {
            e.printStackTrace();
            return "false";
        }
    }

	//个人用户忘记密码
	@RequestMapping("/updatePeople")
	@ResponseBody
	public String updatePeople(String param,String code) { return  comPeopleService.updatePeople(param,code); }

    //登陆
    @RequestMapping("/login")
    @ResponseBody
    public String login(String userName, String passWord, String flag, HttpSession session) {

         return  weUserService.login(userName,passWord,flag,session);

    }
	//退出系统
	@RequestMapping("/exit")
	@ResponseBody
	public String exit(HttpSession session){
		session.removeAttribute("pt_user");
		session.removeAttribute("people_user");
		return "true";
	}
//path 为图片在服务器的绝对路径

@RequestMapping("/getPhoto")
 public   void getPhoto(HttpServletResponse response,@RequestParam("path") String path) throws IOException {
logger.info(path);
    try {
        File file = new File(path);
        FileInputStream fis =  new FileInputStream(file);
        OutputStream out = response.getOutputStream();
        long size = file.length();
        byte[] temp = new byte[(int) size];
        fis.read(temp, 0, (int) size);
        byte[] data = temp;
        response.setContentType("image/png");
        fis.close();
        out.write(data);
        out.flush();
    }catch (IOException c){
        logger.info("文件没找到");
    }

 }


    //注册页面
    @RequestMapping("/regist.html")
    public String regist( ) {
       return  "System/regist";
    }
	//忘记密码
	@RequestMapping("/forget_password.html")
	public String forget_password( ) {
		return  "System/forget_ password";
	}
    //登陆页面
    @RequestMapping("/login.html")
    public String login( ) {
       return  "System/login";
    }
    //个人中心跳转
    @RequestMapping("/main.html")
    public String main(HttpSession session, Model model) {
        ComPtUser comPtUser =new ComPtUser();
        ComPeople comPeople=new ComPeople();
        if(session.getAttribute("people_user")!=null){
            comPeople=comPeopleService.selectById(((ComPeople)session.getAttribute("people_user")).getId());
            model.addAttribute("peopleUser",comPeople);
        }else {
            if(session.getAttribute("pt_user")!=null){
                comPtUser=comPtUserService.selectById(((ComPtUser)session.getAttribute("pt_user")).getId());
                model.addAttribute("ptUser",comPtUser);
            }
        }
       return  "System/UserInfo";
    }


    //个人注册手机号注册重复事件
    @RequestMapping("/phonetxt")
    @ResponseBody
    public Boolean phonetxt(Long phone) {
        return  weUserService.phonetxt(phone);
    }
    //企业注册手机号注册重复事件
    @RequestMapping("/phonetxta")
    @ResponseBody
    public Boolean phonetxta(Long phone) {
        return  weUserService.phonetxta(phone);
    }


}
